package com.ipay.wechat.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ipay.utils.CodecUtils;
import com.ipay.utils.R;

/**
 * 微信首次握手通道
 * @author yangk
 *
 */
@RestController
@RequestMapping("/wechat1")
public class SecurityController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private static String token = "";
	@RequestMapping("/security")
	public R security(@RequestParam Map<String,String> param, HttpServletRequest request){
		logger.info("request name : /security , param:" + param);
		String signature = param.get("signature");
		String timestamp = param.get("timestamp");
		String nonce = param.get("nonce");
//		String echostr = param.get("echostr");
		String hex = CodecUtils.shaHex(StringUtils.join(token,timestamp,nonce));
		if(signature != null && signature.equals(hex)) {
			return R.ok();
		}
		
		return R.error("签名验证失败");
	}
	@RequestMapping("/test")
	public String test(){
		return "r";
	}
	public R test(@RequestBody String requestBody) {
		logger.info(" reuqestBody:" + requestBody);
		return R.ok();
	} 
	

}
